package com.ez.supervise.action.sys;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.MyUtils;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.OrgCheck;
import com.ez.supervise.model.sys.Org;
import com.ez.supervise.service.OrgService;

public class OrgAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long id;
    private Long parentId;
    private String parentName;
    private OrgService orgService;
    private Org org;
    private Page page;

    public void saveOrg() {
	try {
	    if (org.isNew()) {
		Org saveOrg = new Org();
		saveOrg.setName(org.getName());
		saveOrg.setOrgType(org.getOrgType());
		saveOrg.setOrgOrder(org.getOrgOrder());
		saveOrg.setMemo(org.getMemo());
		orgService.insert(saveOrg);
		org.setId(saveOrg.getId());
	    } else {
		orgService.update(org);
	    }
	    Result result = new Result(org.getId());
	    outJsonString(JSONObject.fromObject(result).toString());
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
    }

    public String findAllOrg() {
	page = new Page();
	
	String searchType = getRequest().getParameter("searchType");
	String searchName = getRequest().getParameter("searchName");
	if(StringUtils.isNotBlank(searchName)){
	    page.putCondition("name", searchName.trim());
	} 
	if(StringUtils.isNotBlank(searchType)){
	    page.putCondition("orgType", searchType);
	} 
	int start = Integer.valueOf(getRequest().getParameter("start"));
	int limit = Integer.valueOf(getRequest().getParameter("limit"));
	page.setStart(++start);
	page.setLimit(limit = limit == 0 ? 20 : limit);
	page.addPageInfo();
	page = orgService.findByPage(page);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(orgService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }
    /**
     * @return
     */
    public String deleteOrg() {
	try {
	    if (id != null && id > 0) {
		orgService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateOrg() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strOrgId = getRequest().getParameter("id");
	try {
		
		updateOrg();
		
	    if (id != null && id > 0) {
		Org saveOrg = new Org();
		saveOrg.setId(id);
		BeanUtils.setProperty(saveOrg, fieldName, fieldValue);
		orgService.update(saveOrg);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }
    public String findOrgByType() {
	String orgType = getRequest().getParameter("orgType");
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(orgService.findOrgByType(orgType));

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }
    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	Org org = new Org();
	org.setId(1L);
	org.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(org);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "createTime", "modifyTime" });

	JSONObject jsonObj = JSONObject.fromObject(org, config);
	System.out.println(jsonObj.toString());
    }

    public void setOrgService(OrgService orgService) {
	this.orgService = orgService;
    }

    public OrgService getOrgService() {
	return orgService;
    }

    public void setOrg(Org org) {
	this.org = org;
    }

    public Org getOrg() {
	return org;
    }

    public void setParentId(Long parentId) {
	this.parentId = parentId;
    }

    public Long getParentId() {
	return parentId;
    }

    public void setParentName(String parentName) {
	this.parentName = parentName;
    }

    public String getParentName() {
	return parentName;
    }
}
